package com.atguigu.common.utils;

import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileUtils {

    public static MultipartFile createFromLocalFile(String filePath, String contentType) throws IOException {
        Path path = Paths.get(filePath);
        String fileName = path.getFileName().toString();
        byte[] content = Files.readAllBytes(path);

        return new MockMultipartFile(
                "file",          // 表单字段名（随意）
                fileName,        // 原始文件名
                contentType,     // 如 "image/png"、"text/plain"
                content          // 文件内容字节数组
        );
    }
}